<uni-icons type=""></uni-icons><template>
	<!-- 	<view>
		<image src="/static/image/test/run_test.gif" mode="aspectFill" class="run run_test"></image>
		<button type="primary"  @click="login()">登录</button>
	</view> -->
	<view class="uni-common-mt">
		<view class="logo-box">
			<image src="../../../static/image/loginLogo.png" mode="center" class="login-logo"></image>
		</view>
		<!-- 		<form class="loginForm" @submit="login">
			<view class="uni-form-item uni-column">
				<input class="my_input" id="my_input" name="input" v-model="account" placeholder="请输入账户名" />
			</view>
			<view class="uni-form-item uni-column">
				<input class="my_input" id="my_input" name="input" v-model="password" placeholder="请输入密码" />
			</view>
	
			<view>
				<button class="sub_button"  form-type="submit" type="primary">登录</button>
			</view>
		</form> -->

		<form class="loginForm" @submit="login()">
			<view class="uni-form-item uni-column" style="width: 690rpx;">
				<!-- <input class="my_input" id="my_input" name="input" v-model="username" placeholder="请输入账户名" /> -->
				<uni-easyinput class="my_input" id="my_input" type="clearable" name="input" v-model="userName"
					placeholder="请输入账户名" :styles="styles" trim="all" :clearable="true">
				</uni-easyinput>
			</view>
			<view class="uni-form-item uni-column" style="margin-bottom: 20px; width: 690rpx;">
				<!-- <input class="my_input" id="my_input" password name="input" v-model="password" placeholder="请输入密码" /> -->
				<uni-easyinput class="my_input" id="my_input" type="password" name="input" v-model="password"
					placeholder="请输入密码" :styles="styles" trim="all">
				</uni-easyinput>
			</view>
			<!-- <view class="forget-register">
				<text class="forget">忘记密码</text>
				<text>|</text>
				<text class="register">新用户注册</text>
			</view> -->
			<view>
				<checkbox-group @change="checkboxChange" class="anniu" name="loves">
					<label>
						<checkbox :checked="checkeds" value="" /><text>记住账号密码</text>
					</label>
				</checkbox-group>
			</view>

			<view class="uni-btn-v">
				<button class="sub_button" form-type="submit">登录</button>
			</view>
			<!-- <uni-icons type="cart" size="30"></uni-icons> -->
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: "",
				password: "",
				checkeds: false,
				styles: {
					borderColor: '#EBEFF6'
				},
			}
		},
		onLoad() {
			const cheke = uni.getStorageSync('checked');
			if (cheke.length >= 1) {
				let that = this;
				//缓存的账号
				const HC_name = uni.getStorageSync('HC_name');
				//缓存的密码
				const HC_pass = uni.getStorageSync('HC_pass');

				//有缓存就赋值给文本没有就清空
				if (HC_name && HC_pass) {
					that.userName = HC_name;
					that.password = HC_pass;
					this.checkeds = true;
				} else {
					that.userName = '';
					that.password = '';
					this.checkeds = false
				}
			} else {
				this.checkeds = false
			}
		},
		methods: {
			checkboxChange(e) {
				var checked = e.target.value
				uni.setStorageSync("checked", checked)

			},
			// async getAppConfig() {
			// 	const res = await this
			// 		.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
			// 			url: '/api-mobile/config',
			// 			method: "get",
			// 		})
			// 	// console.log(res)
			// 	uni.setStorageSync('AppConfig', res.body.command)
			// },
			onNavigationBarButtonTap() {
				// console.log("123")
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
			async loginraw() { //async 定义的函数会默认的返回一个Promise对象resolve的值
				// const res = await this
				// 	.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
				// 		url: '/api-portal/login',
				// 		method: "post",
				// 		data: {
				// 			userName: this.username,
				// 			password: this.passwords,
				// 		},
				// 	})
				// if (res.success == true) { //如果登录成功
				// 	// console.log(JSON.stringify(res))
				// 	this.getAppConfig();
				// 	uni.setStorageSync('HC_name', this.account);
				// 	uni.setStorageSync('HC_pass', this.password);
				// 	uni.setStorageSync('loginName', res.body.currentUser.loginName)
				// 	uni.setStorageSync('id', res.body.currentUser.id)
				// 	var loginName = uni.getStorageSync('loginName')
				// 	uni.setStorageSync('x-auth-token', res.body.currentUser.idToken); //获取服务端传递过来的token信息
				// 	//老人登录
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-operate/member/app/security/share/login',
						method: "post",
						data: {
							userName: this.userName,
							password: this.password,
						}
					})
				if (res.data.success == true) {
					// this.getmenu()
					// this.getAppConfig();
					// 缓存账号密码
											uni.setStorageSync('idToken', res.data.body.currentUser.idToken);
											console.log('登录的保存的idToken: '+uni.getStorageSync('idToken'));
					uni.setStorageSync('HC_name', this.userName);
					uni.setStorageSync('HC_pass', this.password);
					uni.setStorageSync('memberName', res.data.body.currentUser.memberName)
					uni.setStorageSync('uniqueCode', res.data.body.currentUser.uniqueCode)
					uni.setStorageSync('id', res.data.body.currentUser.id)
					uni.setStorageSync('memberTypeText', res.data.body.currentUser.memberTypeText)
					uni.setStorageSync('x-auth-token', res.data.body.currentUser.idToken);
					uni.setStorageSync('roomNumber', res.data.body.currentUser.uniqueCode);
					uni.setStorageSync('wristbandNo', res.data.body.currentUser.wristbandNo);//获取一卡通卡号
					// 用于健康数据模块的专业检测link拼接
					
					uni.setStorageSync('phone', res.data.body.currentUser.phone);
					uni.setStorageSync('idCard', res.data.body.currentUser.idCard);
					uni.setStorageSync('imei', res.data.body.currentUser.wristbandImei);
					uni.setStorageSync('genderText', res.data.body.currentUser.genderText);
					uni.setStorageSync('gender', res.data.body.currentUser.gender);
					try {
						const userModule = uni.requireNativePlugin("LJY-UserModule") //调用android中自定义的插件名称
						//执行涂鸦智能登录,使用账户密码登录
						const ret = userModule.login({ //login表示自定义插件的函数名，第一个表示要传递的参数
							userName: "13123481660", //18077799766,ay1627002092336IE868
							password: "12345678" //1qaz2wsx34
						});
						uni.hideLoading(); //加载结束
						// console.log(ret);
						uni.switchTab({
							url: '/pages/user/index'
						});
						uni.showToast({
							title: '登录成功'
						});
					} catch (e) { //如果涂鸦智能无法连接
						uni.hideLoading(); //加载结束
						uni.switchTab({
							url: '/pages/user/index'
						});
					}
				} else {
					uni.showToast({
						title: "账户密码错误",
						mask: true,
						duration: 2000,
						icon: "none"
					});
				}

				// } else {
				// 	uni.showToast({
				// 		title: '登录失败',
				// 		mask: true,
				// 		duration: 2000
				// 	});
				// }
			},
			login(){
				console.log('点击了登录按钮');
			    let params = {
			        userName: this.userName,
			        password: this.password,
			    }
			    uni.request({
			          url: `http://pm.gxtimeloit.com:10030/api-operate/member/app/security/share/login`, 
			          method: 'POST',
			          data: params,
			          success: (res)=>{
						  console.log('登录成功的res： '+JSON.stringify(res));
						  if (res.data.success == true) {
						  	// this.getmenu()
						  	// this.getAppConfig();
						  	// 缓存账号密码
							uni.setStorageSync('idToken', res.data.body.currentUser.idToken);
							console.log('登录的保存的idToken: '+uni.getStorageSync('idToken'));
						  	uni.setStorageSync('HC_name', this.userName);
						  	uni.setStorageSync('HC_pass', this.password);
						  	uni.setStorageSync('memberName', res.data.body.currentUser.memberName)
						  	uni.setStorageSync('uniqueCode', res.data.body.currentUser.uniqueCode)
						  	uni.setStorageSync('id', res.data.body.currentUser.id)
						  	uni.setStorageSync('memberTypeText', res.data.body.currentUser.memberTypeText)
						  	uni.setStorageSync('x-auth-token', res.data.body.currentUser.idToken);
						  	uni.setStorageSync('roomNumber', res.data.body.currentUser.uniqueCode);
						  	uni.setStorageSync('wristbandNo', res.data.body.currentUser.wristbandNo);//获取一卡通卡号
						  	// 用于健康数据模块的专业检测link拼接
						  	
						  	uni.setStorageSync('phone', res.data.body.currentUser.phone);
						  	uni.setStorageSync('idCard', res.data.body.currentUser.idCard);
						  	uni.setStorageSync('imei', res.data.body.currentUser.wristbandImei);
						  	uni.setStorageSync('genderText', res.data.body.currentUser.genderText);
						  	uni.setStorageSync('gender', res.data.body.currentUser.gender);
						  	try {
						  		const userModule = uni.requireNativePlugin("LJY-UserModule") //调用android中自定义的插件名称
						  		//执行涂鸦智能登录,使用账户密码登录
						  		const ret = userModule.login({ //login表示自定义插件的函数名，第一个表示要传递的参数
						  			userName: "13123481660", //18077799766,ay1627002092336IE868
						  			password: "12345678" //1qaz2wsx34
						  		});
						  		uni.hideLoading(); //加载结束
						  		// console.log(ret);
						  		uni.switchTab({
						  			url: '/pages/user/index'
						  		});
						  		uni.showToast({
						  			title: '登录成功'
						  		});
						  	} catch (e) { //如果涂鸦智能无法连接
						  		uni.hideLoading(); //加载结束
						  		uni.switchTab({
						  			url: '/pages/user/index'
						  		});
						  	}
						  } else {
						  	uni.showToast({
						  		title: "账户密码错误",
						  		mask: true,
						  		duration: 2000,
						  		icon: "none"
						  	});
						  }
			          },
			          fail: (err)=>{}
			    })  
			
			},
			//获取菜单
			async getmenu() {
				
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-portal/login',
						method: "post",
						data: {
							 moduleName: "VipApp",
							  password: "1qaz2wsx",
							  userName: "appdemo",
						}
					})
				if (res.success == true) {
					console.log(res)
				
				} else {
					uni.showToast({
						title: "获取菜单失败",
						mask: true,
						duration: 2000,
						icon: "none"
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.logo-box {
		width: 100%;
		height: 328rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.login-logo {
		width: 200rpx;
		height: 200rpx;
		background-color: #DA422A;
		border-radius: 50%;
	}

	/* @import url("/static/style/test.css"); */
	.run {
		width: 750rpx; //750rpx,350rpx
	}

	form.loginForm>view {
		margin: 50rpx;
	}

	.my_input {
		height: 76rpx;
		margin: 10px 5px;
		margin: 0 36rpx 10rpx 36rpx;
		position: relative;
	}

	#my_input::after {
		content: "";
		width: 100%;
		height: 2px;
		background: #DBDBDB;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		margin: 0 auto;
	}

	.forget-register {
		color: #B0B0B0;
		margin: 62rpx 0 84rpx 0;
		padding: 0 36rpx 0 0;
		display: flex;
		justify-content: flex-end;
	}

	.forget-register text:nth-child(2) {
		margin: 0 20rpx 0 20rpx;
	}

	.anniu {
		// margin-left: 60rpx;
		text-align: right;
		margin-right: 20rpx;
	}
</style>
